package me.keiwu.pub.util.wx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jodd.http.HttpResponse;
import me.keiwu.pub.util.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * Created by kei on 05/02/2018.
 */
@Component
public class CustomUtil {

    private static final Logger logger = LoggerFactory.getLogger(CustomUtil.class);

    private static final String CUSTOM_MSG_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send";

    @Value("${wx.appId}")
    private String appId;
    @Value("${wx.secret}")
    private String secret;

    @Autowired
    private AccessTokenUtil accessTokenUtil;


    public void sendCustomTextMsg(String toOpenId, String msg) {
        this.sendCustomMsg(toOpenId, "text", JSON.parseObject(msg));
    }

    public void sendCustomImageMsg(String toOpenId, String msg) {
        this.sendCustomMsg(toOpenId, "image", JSON.parseObject(msg));
    }

    public void sendCustomLinkMsg(String toOpenId, String msg) {
        logger.info("sendCustomLinkMsg toOpenId={}, msg={}", toOpenId, msg);
        this.sendCustomMsg(toOpenId, "link", JSON.parseObject(msg));
    }

    public void sendCustomMsg(String toOpenId, String msgType, JSONObject msg) {
        String token = accessTokenUtil.getAccessToken(appId, secret);
//        String token = "8_sb27Sr2_LzSjoVOof2Y5Y3K_h-xnDtx9IOuI8DirrWMf4WZxitd8AE3JZeia9Yd8rWvfSgJh304A0fALOkwTaQoZCJ4gHRSACoGLc0ZjlX7scqcMuwb476sF-woHXAdAEAKAD";
        String url = CUSTOM_MSG_URL + "?access_token=" + token;
        String jsonBody = this.getMsgBody(toOpenId, msgType, msg);
        System.out.println(jsonBody);
        HttpResponse resp = HttpUtil.post(url, jsonBody);
        System.out.println(resp.body());
        logger.info("sendCustomMsg response: {}", resp.body());
    }

    private String getMsgBody(String toOpenId, String msgType, JSONObject msgJson) {
        JSONObject body = new JSONObject();
        body.put("touser", toOpenId);
        body.put("msgtype", msgType);
        body.put(msgType, msgJson);
        return body.toJSONString();
    }


    public static void main(String[] args) {
        String openId = "ogrwS0e6hXO7VGhPmO8RnKEkxCPU";
        String msg = "你好";
        String appId = "wxf1000eb37c6f40e2";
        String secret = "a75c094294ec1fd3e64429eea861227c";
        String mediaId = "hrTNn_mQrfuBVVWk9_O5j4hnsEVIV7WRnH2ap5EDfl3pPxGX6UGOxJboSn48r9_A";

        CustomUtil util = new CustomUtil();
        util.appId = appId;
        util.secret = secret;

//        util.sendCustomTextMsg(openId, msg);
//        JSONObject json = new JSONObject();
//        json.put("content", msg);
//
//        msg = json.toJSONString();
//        System.out.println(msg);
//        util.sendCustomTextMsg(openId, msg);



        JSONObject json = new JSONObject();
        json.put("title", "title");
        json.put("description", "description");
        json.put("url", "http://www.baidu.com");
        json.put("thumb_url", "http://infinitus-1255411846.cosgz.myqcloud.com/gallery/20171218170935-rgNNlb.png");

        msg = json.toJSONString();
        System.out.println(msg);
        util.sendCustomLinkMsg(openId, msg);
    }

}
